package nz.co.doherty.gwtpbreadcrumbs.client.ui.products;

import nz.co.doherty.gwtpbreadcrumbs.client.NameTokens;
import nz.co.doherty.gwtpbreadcrumbs.client.ui.app.AppPresenter;
import nz.co.doherty.gwtpbreadcrumbs.shared.entity.Product;
import nz.co.doherty.gwtpbreadcrumbs.shared.rpc.product.ProductRetrieveAction;
import nz.co.doherty.gwtpbreadcrumbs.shared.rpc.product.ProductRetrieveResult;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.inject.Inject;
import com.googlecode.objectify.Key;
import com.philbeaudoin.gwtp.dispatch.client.DispatchAsync;
import com.philbeaudoin.gwtp.mvp.client.EventBus;
import com.philbeaudoin.gwtp.mvp.client.PresenterImpl;
import com.philbeaudoin.gwtp.mvp.client.annotations.NameToken;
import com.philbeaudoin.gwtp.mvp.client.annotations.ProxyCodeSplit;
import com.philbeaudoin.gwtp.mvp.client.annotations.TitleFunction;
import com.philbeaudoin.gwtp.mvp.client.proxy.PlaceRequest;
import com.philbeaudoin.gwtp.mvp.client.proxy.ProxyPlace;
import com.philbeaudoin.gwtp.mvp.client.proxy.RevealContentEvent;
import com.philbeaudoin.gwtp.mvp.client.proxy.SetPlaceTitleHandler;

public class ProductInfoPresenter extends
		PresenterImpl<ProductInfoView, ProductInfoPresenter.ProductInfoProxy> {

	@ProxyCodeSplit
	@NameToken(NameTokens.productView)
	public interface ProductInfoProxy extends ProxyPlace<ProductInfoPresenter> {
	}

	private final DispatchAsync dispatcher;
	private Product product = null;
	private String id = null;

	@Inject
	public ProductInfoPresenter(EventBus eventBus, ProductInfoView view,
			ProductInfoProxy proxy, DispatchAsync dispatcher) {
		super(eventBus, view, proxy);
		this.dispatcher = dispatcher;
	}

	@Override
	protected void onBind() {
		super.onBind();
	}

	@Override
	protected void onReveal() {
		super.onReveal();
	}

	@Override
	public void prepareFromRequest(PlaceRequest request) {
		super.prepareFromRequest(request);
		this.id = request.getParameter("id", null);
	}

	@Override
	public PlaceRequest prepareRequest(PlaceRequest request) {
		PlaceRequest pr = super.prepareRequest(request);
		if (this.id != null) {
			pr = pr.with("id", this.id);
		}
		return pr;
	}

	@Override
	protected void revealInParent() {
		RevealContentEvent.fire(eventBus, AppPresenter.TYPE_RevealMainContent,
			this);
	}

	@TitleFunction
	public void getPageTitle( PlaceRequest placeRequest, final SetPlaceTitleHandler handler )  {
		prepareFromRequest( placeRequest );
		if (this.id != null) {
			dispatcher.execute(new ProductRetrieveAction(new Key<Product>(
					Product.class, Long.valueOf(id))),
				new AsyncCallback<ProductRetrieveResult>() {

					@Override
					public void onFailure(Throwable caught) {
						// TODO Auto-generated method stub

					}

					@Override
					public void onSuccess(ProductRetrieveResult result) {
						ProductInfoPresenter.this.product = result.getProduct();
						handler.onSetPlaceTitle(ProductInfoPresenter.this.product
										.getName());
					}
				});

		}
	}
}
